package action

import (
	"core/corestruct"
	"core/coreutil"
	"core/myerrors"
	"filesvr/api/fsimg/service"
	"filesvr/filesvrinfo"
	"reflect"
	"strconv"
)

type DownAction struct {
}

func (p *DownAction) Do(ctx *corestruct.HttpContext) error {
	var c = ctx.Http
	var sid = c.Query("id")
	if "" == sid {
		return myerrors.New("无效的请求")
	}
	var id, _ = strconv.ParseInt(sid, 10, 64)
	m, filedata, err := service.MainService.Get(id, ctx)
	if err != nil {
		return myerrors.NewByErr(err)
	}
	c.Header("Content-Type", "application/octet-stream")
	c.Header("Content-Disposition", "attachment; filename=\""+m.Name+"\"")

	c.Status(200)
	c.Writer.Write(*filedata)

	return nil
}

func DownActionInit() {
	var mact = &DownAction{}
	var maction = corestruct.Action{
		Method: "",

		FullUrl: service.URLPrev + "/down",
		Name:    "下载" + PAGEName,
		Ename:   "",
		Memo:    "",

		PinType:    "",
		PinIslist:  false,
		PoutType:   "",
		PoutIslist: false,
		PoutIspage: false,

		AuthType: corestruct.AuthType_NONE,

		LogNeed:     false,
		LogPin:      false,
		LogPout:     false,
		LogTemplete: "",

		PrjName: filesvrinfo.PRJ_NAME,

		Value: reflect.ValueOf(mact),
	}
	coreutil.ActionUtil.Add(maction)

}
